﻿//============================================================================================
/*
 * @brief 有限状态机 - 状态机。
 * 
 *********************************************************************************************/

namespace Imagine.Infrastructure.FiniteStateMachines
{
    /// <summary>
    /// 有限状态机 - 状态机。
    /// </summary>
    public interface IFSMStateMachine
    {
        void Enable();
        void Disable();
        void AddState(IFSMState      state);
        void SetDefaultState(string? name);
        void ChangeState(string?     stateName);
        void Execute();
    }

    /// <summary>
    /// 有限状态机 - 状态机。
    /// </summary>
    public class FSMStateMachine : FSMGroupState, IFSMStateMachine
    {
        private bool _isInit;
        private bool _enabled = true;

        private FSMStateMachine(string? name) : base(name) { }

        public new static IFSMStateMachine Create(string? name) => new FSMStateMachine(name);

        public void Enable() => _enabled = true;

        public void Disable() => _enabled = false;

        public void Execute()
        {
            if (_enabled == false)
                return;

            if (_isInit == false)
            {
                _isInit = true;
                OnEnter();
            }

            OnUpdate();
        }
    }
}